package com.google.code.sysarch.v4.graphical;

import com.google.code.sysarch.v4.vrml_loader.VRML_Model;

public class Fish {

	VRML_Model model;	
	int from, to;
	double advance;
	double animspeed;
	double speed;
	int direction = 1;
	float[] keys;
	float[] center = { 0,0,0 };
	float angle;
	float radius;
	
	Fish(VRML_Model model) {
		this.from = 0;
		this.to = 1;
		this.speed = 0;
		this.animspeed = 0;
		this.advance = 0;
		this.angle = 0;
		this.model = model;
		this.radius = 100;
		this.keys = model.getMorphKeys();
	}
	
	public void setDirection(int dir) {
		this.direction = dir;
	}
	
	public void setRadius(float radius) {
		this.radius = radius;
	}
	
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	
	public void setAnimationSpeed(double speed) {
		this.animspeed = speed;
	}
	
	public void setCenter(float x, float y, float z) {
		this.center[0] = x;
		this.center[1] = y;
		this.center[2] = z;
	}
	
	public void render() {
		model.morph(from, to, advance);
	}
	
	public void update(double delta) {
		advance += animspeed*delta;
		if (advance >= 1) {
			advance = 0;
			from++;
			to++;
			if (to >= keys.length) {
				from = 0;
				to = 1;
			}
		}
		this.angle += this.direction*(180*this.speed*delta)/(Math.PI*this.radius);
		this.angle = (this.angle+360) % 360;
		this.model.rotate(0, 180-this.angle-90*this.direction, 0);
		this.model.translate((float)Math.cos(this.angle*Math.PI/180)*radius+center[0], center[1], (float)Math.sin(this.angle*Math.PI/180)*radius+center[2]);
	}
	
}
